<?
namespace app\admin\admin;

use xto\data\entities\ConfigInfo;
use xto\data\service\ConfigService;
use xto\data\entities\PayInfo;
use xto\data\service\PayService;
use xto\Util;
use xto\App;
use xto\membership\core\CreateUserStatus;
use xto\membership\core\UserRole;
use xto\membership\context\Users;
use xto\membership\core\UserHelper;
use xto\membership\core\RoleHelper;

use think\Cache;
use think\Request;

class Api extends BaseController{

	public function config(){
		$value=input('value');
		$name=input('name');
		$arr=[
			'name'=>$name,
			'value'=>$value
		];
		$str="用户更新配置";
		App::writelog($str);
		$result=ConfigService::save($arr);
		
		return Util::message('更新成功',$result);
	}

	public function saveconfig(){
		$t=Request::instance();	 
		foreach ($t->param() as $key => $value) {
			$arr=[
				'name'=>$key,
				'value'=>$value
			];
			$result=ConfigService::save($arr);
		}
		return Util::message('更新成功',true);
	}

	public function addpay(){

		$type=input('type');
		if (empty($type)) { 
			return Util::message('类型为空',false);
		}
		$c=PayService::getPay($type);
		if(empty($c)){
			$c=new PayInfo();
		} 
		$result=array();
		$data=\xto\plugins\PayPlugins::instance()->getPlugin($type)->getData();
		foreach ($data as $key => $value) {
			if(is_bool($value)){
				$result[$key]=(boolean)input($key);
			}else{
				$result[$key]=input($key);
			}
		}

		$c->config=json_encode($result);
		$c->paytype=$type;
		if(PayService::create($c)){
			return Util::message('保存成功',true);
		}
		return Util::message('保存失败',false);
	}

	public function smstest(){
		$arr = Util::message('保存失败',false);
		$mobile		=input('testmobile');
		$content	=input('testcontent');
		$c=json_decode($this->config->smssender);
		if (!empty($this->config->smstype)) {
			$p=\xto\plugins\SMSSender::createInstance($this->config->smstype,$c);
	        if (!empty($p)) {
	            $r=$p->send($mobile,$content); 
	            if($r){
		            return Util::message('保存成功',true);
		        }
				
	        } 
	    }
	    return $arr;
	}

	public function smssender(){
		//$c=ConfigService::getConfig();
		$type=input('type');
 		if (empty($type)) { 
			return Util::message('类型为空',false);
		}
		$result=array();
		$data=\xto\plugins\SMSPlugins::instance()->getPlugin($type)->getData();
		foreach ($data as $key => $value) {

			if(is_bool($value)){
				$result[$key]=(boolean)input($key);
			}else{
				$result[$key]=input($key);
			}
		} 
		$h=[
			'name'=>'smssender',
			'value'=>json_encode($result)
		];
		ConfigService::save($h);
		$d=[
			'name'=>'smstype',
			'value'=>$type
		];
		ConfigService::save($d);
		return Util::message('保存成功',true);
	}

	public function emailsender(){
		$type=input('type');
		if (empty($type)) {
			return Util::message('类型为空',false);
		}
		$result=array();
		$data=\xto\plugins\EmailPlugins::instance()->getPlugin($type)->getData();
		foreach ($data as $key => $value) {
			if(is_bool($value)){
				$result[$key]=(boolean)input($key);
			}else{
				$result[$key]=input($key);
			}
		} 
		$h=[
			'name'=>'emailsender',
			'value'=>json_encode($result)
		];
		ConfigService::save($h);
		$d=[
			'name'=>'emailtype',
			'value'=>$type
		];
		ConfigService::save($d);
		return Util::message('保存成功',true);
	}

	public function emailtest(){
		$arr = Util::message('发送失败',false);
		$email		=input('testemail');
		$content	=input('testcontent');
		$c=json_decode($this->config->emailsender);
		if (!empty($this->config->emailtype)) {
			$p=\xto\plugins\EmailSender::createInstance($this->config->emailtype,$c);
	        if (!empty($p)) {
	            $r=$p->send($email,'测试',$content); 
	            if($r){
		            return Util::message('发送成功',true);
		        }
				
	        } 
	    }
	    return $arr;
	}


	public function osssender(){
		$type=input('type');
		if (empty($type)) {
			return Util::message('类型为空',false);
		}
		$result=array();
		$data=\xto\plugins\OSSPlugins::instance()->getPlugin($type)->getData();
		foreach ($data as $key => $value) {
			if(is_bool($value)){
				$result[$key]=(boolean)input($key);
			}else{
				$result[$key]=input($key);
			}
		} 

		$h=[
			'name'=>'osssender',
			'value'=>json_encode($result)
		];
		ConfigService::save($h);
		$d=[
			'name'=>'osstype',
			'value'=>$type
		];
		ConfigService::save($d);
		return Util::message('保存成功',true); 
	}

	public function createmanager(){
		$username=input('username');
		$password=input('password');
		$nickname=input('nickname');
		$email=input('email');
		$roleid=input('funrole');
		$is_admin=input('is_admin');
		if(!$is_admin){
			if (empty($roleid)) {
				return Util::message('请至少选择一个角色',false);
			}
		} 

		$user=new \xto\membership\context\Manager();
		$user->username 	= $username;
		$user->password 	= $password;
		$user->email 		= $email;
		$user->description 	= $nickname;
		$user->is_approved	= true;
		$user->is_admin 	= $is_admin;
		$user->funrole 		= $roleid;

		$result=Users::createUser($user);
		switch ($result) {
			case CreateUserStatus::Created:
				return Util::message('保存成功'.$email,true);
			case CreateUserStatus::DuplicateUsername:
				return Util::message('无效用户名',false);
			default:
				return Util::message('保存失败'.$result,false);
		}
	}

	public function editmanager(){
		$id=input('pid');
		$password=input('password');
		$email=input('email');
		$is_approved=input('is_approved');
		$is_admin=(bool)input('is_admin');
		$roleid=input('funrole');
		if (empty($email)) {
			return Util::message('邮箱不能为空',false);
		}
		if(!$is_admin){
			if (empty($roleid)) {
				return Util::message('请至少选择一个角色',false);
			}
		} 

		$user=Users::getUser($id,'',false);
		if (!empty($user)) {
			$user->email 		= $email;
			$user->is_approved 	= $is_approved;
			$user->is_admin 	= $is_admin;
			if(!empty($password)){
				$user->password =$password;
			}else{
				$user->password ='';
			}
			$user->funrole 		= $roleid;
			if(Users::updateUser($user)){
				$re=false;
				if(!empty($password)){
					$re=UserHelper::changeLoginPassword($user->userid,$password);
				}
				UserHelper::deleteUserRoles($user->userid);//先删除所有
				RoleHelper::addUserToRole($user->userid,$roleid);
				return Util::message('修改成功'.$re,true);
			}
		} 
		return Util::message('修改失败',false);
	}

	public function createrole(){
		$name=input('name');

		$role=new \xto\membership\core\IRole();
		$role->name=$name;

		if(\xto\membership\core\RoleHelper::createRole($role)){
			return Util::message('保存成功',true);
		}
		return Util::message('保存失败',false);
	}

	public function users_roles_delete(){
		$id=input('id');
		if(\xto\membership\core\RoleHelper::deleteRole($id)){
			return Util::message('删除成功',true);
		}
		return Util::message($id,false);
	}

	public function uploader(){ 
		$file = request()->file('file'); 
		if (empty($file)) {
			return Util::message('请选文件',false);
		}
		return json(\xto\Util::oss($file));  
	}

	public function logs_index_delete(){
		$id=input('id');
		if(\xto\data\service\LogService::delete($id)){
			return Util::message('删除成功',true);
		}
		return Util::message('删除失败',false);
	}


	public function createmember(){
		$username=input('username');
		$password=input('password');
		$nickname=input('nickname');
		$email=input('email');

		$user=new \xto\membership\context\Member();
		$user->username 	= $username;
		$user->password 	= $password;
		$user->email 		= $email; 
		$user->is_approved	= true;

		$result=\xto\membership\context\Users::createUser($user);
		switch ($result) {
			case CreateUserStatus::Created:
				return Util::message('保存成功'.$email,true);
			case CreateUserStatus::DuplicateUsername:
				return Util::message('无效用户名',false);
			default:
				return Util::message('保存失败'.$result,false);
		}
	}


	public function editmember(){
		$id=input('pid');
		$password=input('password');

		$user=\xto\membership\context\Users::getUser($id);
		if (!empty($user)) {
			$user->email 			= input('email');
			$user->nickname 		= input('nickname');
			$user->realname 		= input('realname');
			$user->identify_card 	= input('identify_card');
			$user->address 			= input('address');
			$user->zipcode 			= input('zipcode');
			$user->phone 			= input('phone');
			$user->mobile 			= input('mobile');
			$user->qq 				= input('qq');
			$user->wangwang 		= input('wangwang');
			$user->wechat 			= input('wechat');
			$user->alipay 			= input('alipay'); 
			if(!empty($password)){
				$user->password =$password;
			}else{
				$user->password ='';
			}
			if(\xto\membership\context\Users::updateUser($user)){
				$re=false;
				if(!empty($password)){
					$re=\xto\membership\core\UserHelper::changeLoginPassword($user->userid,$password);
				}
				return Util::message('修改成功'.$re,true);
			}
		} 
		return Util::message('修改失败',false);
	}

	public function createwallet(){
		$username 	=input('username');
		$amount 	=input('amount');
		$user=Users::getuser(0,$username,false);
		if ($user->userrole==UserRole::Member) { 
			if(\xto\data\service\WalletService::create($user,$amount,0,'后台添加')){
				//清理缓存
				Users::clearUserCache($user);
				return Util::message('添加成功',true);
			} 
		}
		return Util::message('添加失败',false);
	}

	public function createpoint(){
		$username 	=input('username');
		$amount 	=input('amount');
		$user=Users::getuser(0,$username,false);
		if ($user->userrole==UserRole::Member) { 
			if(\xto\data\service\PointService::create($user,$amount,0,'后台添加')){
				Users::clearUserCache($user);
				return Util::message('添加成功',true);
			} 
		}
		return Util::message('添加失败',false);
	}

	public function createsplittin(){
		$username 	=input('username');
		$amount 	=input('amount');
		$user=Users::getuser(0,$username,false);
		if ($user->userrole==UserRole::Member) { 
			if(\xto\data\service\SplittinService::create($user,$amount,0,'后台添加')){
				Users::clearUserCache($user);
				return Util::message('添加成功',true);
			} 
		}
		return Util::message('添加失败',false);
	}

	public function allot(){
		$ids 	=input("ids");
		$roleid =input("roleid");
		$arr=array_unique(explode(',',$ids)); 


		if(\xto\membership\core\RoleHelper::addRoleFunctions($roleid,$arr)){
			return Util::message('添加成功',true);
		}
		return Util::message('添加失败',false);
	}
}